package io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by pc on 2015/4/29.
 */
public class CheckLog {
    private static ArrayList<String> fileList = new ArrayList<String>();

    public static void listFilesForFolder(final File folder) {
        for (final File fileEntry : folder.listFiles()) {
            if (fileEntry.isDirectory()) {
                listFilesForFolder(fileEntry);
            } else {
                fileList.add(fileEntry.getAbsolutePath());
                System.out.println(fileEntry.getName());
            }
        }
    }
    public static void main(String[] args)  {
        // String to be scanned to find the pattern.
//        String pattern = "(.*)(\\d+)(.*)";
//        Pattern p = Pattern.compile("-?\\d{32}");
////        Matcher m = p.matcher("There are more than -2 and less than 12 numbers here");
//        Matcher m = p.matcher("2015-10-22 22:32:15 ERROR [MW_SYNC_LOTUS_MONITOR] - updateSalesCommission:AE1C6E65F24ED1E348257EE6004018A2\n");
//        while (m.find()) {
//            System.out.println(m.group());
//        }

        Set<String> hs = new HashSet<String>();

        final File folder = new File("D://logs//");
        listFilesForFolder(folder);
        String errorChk = "MW_SYNC_LOTUS_MONITOR";

        for (String fName : fileList){
            // Open the file
            FileInputStream fstream ;
            try {
                fstream = new FileInputStream(fName);
                BufferedReader br = new BufferedReader(new InputStreamReader(fstream));

                String strLine;
                //Read File Line By Line
                while ((strLine = br.readLine()) != null)   {
                    // Print the content on the console
                    int chkstart = strLine.lastIndexOf("-");
                    int brackStart = strLine.indexOf("(");
                    if (strLine.indexOf(errorChk) > 5) {
                        String id="";
                        if (brackStart > chkstart && chkstart > 0) {
//                            System.out.println( " checking " + strLine);
//                              id = strLine.substring(brackStart+1, brackStart+33 );
//                            System.out.println(id + " ----------- " + strLine);
                        } else if (strLine.indexOf(errorChk) > 5 && chkstart > strLine.indexOf(errorChk)) {
                            int start = strLine.lastIndexOf(":");
                               id = strLine.substring(start + 1);
//                            System.out.println(id);
                        System.out.println (id + " ===== " + strLine);
                        }
                        hs.add(id);
                    }

                }
                //Close the input stream
                br.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
//        for (String id: hs){
//           System.out.println(id+",");
//        }
    }
}

